package com.papercutting.platform.controller;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.dto.request.ChangePasswordRequest;
import com.papercutting.platform.dto.request.LoginRequest;
import com.papercutting.platform.dto.request.ProfileUpdateRequest;
import com.papercutting.platform.dto.request.UserCreateRequest;
import com.papercutting.platform.dto.request.UserUpdateRequest;
import com.papercutting.platform.dto.request.WechatLoginRequest;
import com.papercutting.platform.dto.response.LoginResponse;
import com.papercutting.platform.dto.response.UserResponse;
import com.papercutting.platform.service.AuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 认证控制器 - 重构为符合三层架构规范
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@Validated
public class AuthController {
    
    private final AuthService authService;
    
    /**
     * 网页登录
     */
    @PostMapping("/web-login") 
    public Result<LoginResponse> webLogin(@Valid @RequestBody LoginRequest request) {
        log.info("网页登录请求: {}", request.getUsername());
        
        LoginResponse response = authService.login(request);
        
        return Result.success("登录成功", response);
    }
    
    /**
     * 微信登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> wechatLogin(@Valid @RequestBody WechatLoginRequest request) {
        log.info("微信登录请求");
        
        LoginResponse response = authService.wechatLogin(request);
        
        return Result.success("登录成功", response);
    }
    
    
    /**
     * 获取用户信息
     */
    @GetMapping("/userinfo")
    public Result<UserResponse> getUserInfo(@RequestHeader("Authorization") String authHeader) {
        String token = authHeader.replace("Bearer ", "");
        
        UserResponse userResponse = authService.getUserInfoByToken(token);
        
        return Result.success(userResponse);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/userinfo")
    public Result<UserResponse> updateUserInfo(
            @RequestHeader("Authorization") String authHeader,
            @Valid @RequestBody ProfileUpdateRequest request) {
        String token = authHeader.replace("Bearer ", "");

        UserResponse userResponse = authService.updateProfile(token, request);

        return Result.success("更新成功", userResponse);
    }
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<LoginResponse> register(@Valid @RequestBody UserCreateRequest request) {
        log.info("用户注册请求: {}", request.getNickname());
        
        LoginResponse response = authService.register(request);
        
        return Result.success("注册成功", response);
    }
    
    /**
     * 修改密码
     */
    @PutMapping("/password")
    public Result<Void> changePassword(
            @RequestHeader("Authorization") String authHeader,
            @Valid @RequestBody ChangePasswordRequest request) {
        String token = authHeader.replace("Bearer ", "");

        authService.changePassword(token, request);

        return Result.success("密码修改成功");
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result<Void> logout(@RequestHeader("Authorization") String authHeader) {
        String token = authHeader.replace("Bearer ", "");

        authService.logout(token);

        return Result.success("登出成功");
    }
}